<?php

/**
 * author actions.
 *
 * @package    quotes
 * @subpackage author
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class authorActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->pager = AuthorQuery::create()
      ->filterByIsActive(true)
      ->orderByLetter()
      ->paginate($request->getParameter('page',1), 12);
    $this->getResponse()->addMeta('keywords', Tools::getKeywords($this->pager));
  }

  public function executeIndexCategories(sfWebRequest $request)
  {
    $this->pager = CategoryQuery::create()
      ->orderByName()
      ->paginate($request->getParameter('page',1), 12);
    $this->getResponse()->addMeta('keywords', Tools::getKeywords($this->pager));
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->forward404Unless($this->author = AuthorQuery::create()->retrieveBySlug($request->getParameter('slug')));
    $this->pager = QuoteQuery::create()
      ->filterByIsActive(true)
      ->filterByAuthorId($this->author->getId())
      ->paginate($request->getParameter('page', 1), 12);

    $this->getResponse()->setTitle('Addictive Quotes - '.$this->author->getName());
    $keywords_ini = $this->author->getName().',';
    $this->getResponse()->addMeta('keywords', Tools::getKeywords($this->author->getCategorys(), $keywords_ini));
    $tr_cont = $this->author->getTrimmedContent();
    if ($this->author->getContent())
      $this->getResponse()->addMeta('description', $tr_cont['value']);
  }

  public function executeByLetter(sfWebRequest $request)
  {
    $letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'w', 'x', 'y', 'z');
    $quotes = array();
    $authors = array();
    foreach ($letters as $letter)
    {
      $authors[$letter] = AuthorQuery::create()
        ->filterByIsActive(true)
        ->filterByLetter($letter)
        ->findOne();
      $quotes[$letter] = QuoteQuery::create()
        ->filterByIsActive(true)
        ->findOneByAuthorId($authors[$letter]->getId());
    }

  }

  public function executeShowByLetter(sfWebRequest $request)
  {
    $this->letter = $request->getParameter('letter');
    $this->forward404Unless($this->letter);

    $this->pager = AuthorQuery::create()
      ->filterByIsActive(true)
      ->filterByLetter($this->letter)
      ->paginate($request->getParameter('page', 1), 10);

    $this->getResponse()->setTitle('Addictive Quotes - Authors by letter ('.ucwords($this->letter).')');
    $this->getResponse()->addMeta('keywords', Tools::getKeywords($this->pager, 'Authors by letter ('.ucwords($this->letter).'),'));
  }

  public function executeShowByCategory(sfWebRequest $request)
  {
    $this->category = CategoryQuery::create()->findOneBySlug($request->getParameter('slug'));
    $this->forward404Unless($this->category);
    $this->pager = AuthorQuery::create()
      ->filterByIsActive(true)
      ->useAuthorCategoryQuery()
        ->filterByCategoryId($this->category->getId())
      ->endUse()
      ->paginate($request->getParameter('page', 1), 10);

    $this->getResponse()->setTitle('Addictive Quotes - '.$this->category->getName());
    $this->getResponse()->addMeta('keywords', Tools::getKeywords($this->pager, $this->category->getName().','));
  }
}
